//
//  KRReadListCell.m
//  cookBook
//
//  Created by kuroken on 16/1/12.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRReadListCell.h"
#import "KRReadList.h"
#import <UIImageView+WebCache.h>
#import "KRConst.h"

// cell的标识
static NSString * const reuseIdentifier = @"readList";

@implementation KRReadListCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRReadListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell = [[KRReadListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 该属性控制分割线的位置和长度
//        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 20);

        self.textLabel.numberOfLines = 0;
        self.textLabel.lineBreakMode = NSLineBreakByClipping;
        self.detailTextLabel.textColor = [UIColor lightGrayColor];
        self.textLabel.font = [UIFont systemFontOfSize:15];
        self.detailTextLabel.font = [UIFont systemFontOfSize:13];
//        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//        self.imageView.clipsToBounds = YES;
    }
    return self;
}

- (void)setReadList:(KRReadList *)readList
{
    _readList = readList;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:readList.img] placeholderImage:[UIImage imageNamed:@"default_r172-118"]];
    [self.textLabel setText:readList.title];
    [self.detailTextLabel setText:readList.intro];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // imageView.frame
    CGFloat imageX = 10;
    CGFloat imageY = 5;
    CGFloat imageW = 100;
    CGFloat imageH = baseCellHeight - imageY * 2;
    
    if (self.frame.size.height > baseCellHeight) {
        imageY = 5 + cellPadding;
        imageH = baseCellHeight + cellPadding * 2 - imageY * 2;
    }
    
    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    
    // textLabel.frame
    CGFloat textX = CGRectGetMaxX(self.imageView.frame) + 15;
    CGFloat textY = imageY;
    CGFloat textW = KRReadListWidth - textX - 25;
    CGFloat textH = 44;
    self.textLabel.frame = CGRectMake(textX, textY, textW, textH);
    
    // detailTextLabel.origin
    CGFloat detailX = textX;
    CGFloat detailY = CGRectGetMaxY(self.imageView.frame) - self.detailTextLabel.size.height;
    self.detailTextLabel.width = KRReadListWidth - detailX - 25;
    self.detailTextLabel.origin = CGPointMake(detailX, detailY);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
